﻿/**
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 **/
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Peter.Ftp.Data;

namespace Peter.Ftp.Tests.Data
{
   /// <summary>
   /// Test cases for the crypto class.
   /// </summary>
   [TestClass]
   public class CryptoTests
   {
      private const string SHARED_SECRET = "Ssshhh";
      private const string TEXT_TO_ENCRYPT = "TestText";
      private const string ENCRYPTED_TEXT = "EAAAAPKyTwpZX9iAq6NuW6WIeBOt8dSaQDoGLjJACjEhXhaJ";

      /// <summary>
      /// Verifies that the encrypt method requires some text to encrypt.
      /// </summary>
      [TestMethod]
      [ExpectedException (typeof (ArgumentNullException))]
      public void TestEncryptEmptyText ()
      {
         Crypto.EncryptStringAes (null, null);
      }

      /// <summary>
      /// Verifies that the encrypt method requires a shared secret key.
      /// </summary>
      [TestMethod]
      [ExpectedException (typeof (ArgumentNullException))]
      public void TestEncryptEmptySecret ()
      {
         Crypto.EncryptStringAes (TEXT_TO_ENCRYPT, null);
      }

      /// <summary>
      /// Verifies that the decrypt method requires ciphered text.
      /// </summary>
      [TestMethod]
      [ExpectedException (typeof (ArgumentNullException))]
      public void TestDecryptEmptyText ()
      {
         Crypto.DecryptStringAes (null, null);
      }

      /// <summary>
      /// Verifies that the decrypt method requires a shared secret key.
      /// </summary>
      [TestMethod]
      [ExpectedException (typeof (ArgumentNullException))]
      public void TestDecryptEmptySecret ()
      {
         Crypto.DecryptStringAes (ENCRYPTED_TEXT, null);
      }

      /// <summary>
      /// Verifies that the encrypt method produces data.
      /// </summary>
      [TestMethod]
      public void TestEncrypt ()
      {
         var data = Crypto.EncryptStringAes (TEXT_TO_ENCRYPT, SHARED_SECRET);
         Assert.IsFalse (string.IsNullOrEmpty (data), "The encrypt method did not produce ciphered text.");
      }

      /// <summary>
      /// Verifies that the decrypt method produces unencrypted text.
      /// </summary>
      [TestMethod]
      public void TestDecrypt ()
      {
         var data = Crypto.DecryptStringAes (ENCRYPTED_TEXT, SHARED_SECRET);
         Assert.IsFalse (string.IsNullOrEmpty (data), "The decrypt method did not produce unciphered text.");
         Assert.AreEqual (TEXT_TO_ENCRYPT, data, "The decrypted text is different than expected.");
      }
   }
}
